package com.key.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.common.base.RespResult;
import com.key.common.base.TranException;
import com.key.common.constants.Constants;
import com.key.modules.dto.*;
import com.key.modules.entity.Complain;
import com.key.modules.entity.WException;
import com.key.modules.mapper.WExceptionMapper;
import com.key.modules.service.WExceptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class WExceptionServiceImpl implements WExceptionService {

    @Autowired
    private WExceptionMapper wExceptionMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public RespResult addWException(WExceptionDTO wExceptionDTO) {
        // log.info("{}==添加运单异常", SecurityUtils.getSubject().getPrincipal());
        Long e_no = redisTemplate.opsForValue().increment("e_no");
        WException wException=new WException();
        BeanUtil.copyProperties(wExceptionDTO,wException);
        wException.setENo(String.format("%s%05d", Constants.WEPRE, e_no));
        wException.setCreateTime(new Date());
        int i = wExceptionMapper.insert(wException);
        if (i>0){
            log.info("{}==运单异常添加成功",wException.toString());
            return RespResult.success();
        }
        return RespResult.error("添加失败");
    }

    @Override
    public RespResult findAllWException(WConditionDTO wConditionDTO) {
        Page<WException> page=new Page<>(wConditionDTO.getPageNum(),wConditionDTO.getPageSize());
        LambdaQueryWrapper<WException> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(wConditionDTO.getTNo()),WException::getTNo,wConditionDTO.getTNo())
                .eq(wConditionDTO.getEType()!=null,WException::getEType,wConditionDTO.getEType())
                .eq(wConditionDTO.getState()!=null,WException::getState,wConditionDTO.getState())
                .ge(wConditionDTO.getStartTime()!=null,WException::getCreateTime,wConditionDTO.getStartTime())
                .le(wConditionDTO.getEndTime()!=null,WException::getCreateTime,wConditionDTO.getEndTime())
                .orderBy(wConditionDTO.getOrder()!=null,wConditionDTO.getOrder()==Constants.ASC,WException::getCreateTime);
        Page<WException> wExceptionPage = wExceptionMapper.selectPage(page,lambdaQueryWrapper);
        System.out.println("当前页码数："+wExceptionPage.getCurrent());
        System.out.println("每页显示条数："+wExceptionPage.getSize());
        System.out.println("总条数："+wExceptionPage.getTotal());
        System.out.println("总页数："+wExceptionPage.getPages());
        System.out.println("查询数据："+wExceptionPage.getRecords());
        return RespResult.success(wExceptionPage);
    }

    @Override
    public RespResult findWExceptionById(Integer id) {
        WException wException = wExceptionMapper.selectById(id);
        if (wException!=null){
            return RespResult.success(wException);
        }
        return RespResult.error("查询失败");
    }

    @Override
    public RespResult updateWException(WExceptionDTO wExceptionDTO) {
        // log.info("{}==修改运单异常", SecurityUtils.getSubject().getPrincipal());
        WException wException=new WException();
        BeanUtil.copyProperties(wExceptionDTO,wException);
        int i = wExceptionMapper.updateById(wException);
        if (i>0){
            log.info("{}==修改成功", wException.toString());
            return RespResult.success();
        }
        return RespResult.error("修改失败");
    }

    @Override
    public RespResult deleteWException(List<Integer> ids) {
        // log.info("{}==删除运单异常", SecurityUtils.getSubject().getPrincipal());
        try {
            wExceptionMapper.deleteBatchIds(ids);
            log.info("{}==删除成功",ids);
            return RespResult.success();
        }catch (Exception e){
            throw new TranException("删除失败");
        }
    }

    @Override
    public RespResult updateHState(HComplainDTO hComplainDTO) {
        WException wException=new WException();
        BeanUtil.copyProperties(hComplainDTO,wException);
        wException.setState(Constants.CSTATE);
        int i = wExceptionMapper.updateById(wException);
        if (i>0){
            log.info("{}==处理成功", wException.toString());
            return RespResult.success();
        }
        return RespResult.error("处理失败");
    }

    @Override
    public RespResult updateFState(FComplainDTO fComplainDTO) {
        WException wException =new WException();
        BeanUtil.copyProperties(fComplainDTO,wException);
        wException.setState(Constants.FSTATE);
        int i = wExceptionMapper.updateById(wException);
        if (i>0){
            log.info("{}==完结成功", wException.toString());
            return RespResult.success();
        }
        return RespResult.error("完结失败");
    }
}
